<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
<head>
  <link href="https://gmpg.org/xfn/11" rel="profile">
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="generator" content="Hugo 0.68.3" />

  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>python基础字典常用方法 &middot; 我的博客</title>
  <meta name="description" content="" />

  
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/print.css" media="print">
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/poole.css">
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/syntax.css">
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/hyde.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Abril+Fatface|PT+Sans:400,400i,700">


  
  <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/apple-touch-icon-144-precomposed.png">
  <link rel="shortcut icon" href="/favicon.png">

  
  
</head>

  <body class="theme-base-09 ">
  <aside class="sidebar">
  <div class="container sidebar-sticky">
    <div class="sidebar-about">
      <a href="/my_technology_blog/"><h1>我的博客</h1></a>
      <p class="lead">
       这里是任永刚的个人博客 
      </p>
    </div>

    <nav>
      <ul class="sidebar-nav">
        <li><a href="/my_technology_blog/">Home</a> </li>
        
      </ul>
    </nav>

    <p>&copy; 2020. All rights reserved. </p>
  </div>
</aside>

    <main class="content container">
    <div class="post">
  <h1>python基础字典常用方法</h1>
  <time datetime=2020-04-21T00:00:00Z class="post-date">Tue, Apr 21, 2020</time>
  <h2 id="13-字典常用方法">1.3 字典常用方法</h2>
<pre><code>#1. clear方法清除字典中所有的项，这是一个原地操作，所以无返回值（或则说返回None）
d = {'name':&quot;tom&quot;}
d.clear()
print(d)                                         #the result : {}

#2. fromkeys方法使用给定的键建立新的字典，每个键都对应一个默认的值None
print({}.fromkeys(['name','age']))              #the result　：　{'age': None, 'name': None}

#3. get方法是个更宽松的访问字典项的方法，如果试图访问字典中不存在的项时不会报错仅会      返回：None
d = {'Tom':8777,'Jack':8888,'Fly':6666}
print(d.get('Tom'))                              #the result ：     8777
print(d.get('not_exist'))                       #the result ：     None

#4. for循环字典的三种方法
d = {'Tom':8777,'Jack':8888,'Fly':6666}
for k,v in d.items():
    print(k,v)
for k in d.values():
    print(k)
for k in d.keys():
    print(k)

#5. pop方法用于获得对应与给定键的值，然后将这个”键-值”对从字典中移除
d = {'Tom':8777,'Jack':8888,'Fly':6666}
v = d.pop('Tom')
print(v)                    #8777


#6. setdefault方法在某种程度上类似于get方法，能够获得与给定键相关联的值，除此之外，setdefault还能在字典中不含有给定键的情况下设定相应的键值
d = {'Tom':8777,'Jack':8888,'Fly':6666}
d.setdefault('Tom')                           #the result ： 8777
print(d.setdefault('Test'))                  #the result ： None
print(d)                                      #{'Fly': 6666, 'Jack': 8888, 'Tom': 8777, 'Test': None}

#7. update方法可以利用一个字典项更新另一个字典，提供的字典中的项会被添加到旧的字典中，如有相同的键则会被覆盖
d = {'Tom':8777,'Jack':8888,'Fly':6666}
a = {'Tom':110,'Test':119}
d.update(a)
print(d)                                       #the result ：{'Fly': 6666, 'Test': 119, 'Jack': 8888, 'Tom': 110}

#8. 将两个列表组合成字典
keys = ['a', 'b']
values = [1, 2]
print(dict(zip(keys,values)))                                      # {'a': 1, 'b': 2}
</code></pre>

</div>


    </main>

    
      
    
  </body>
</html>
